#!/bin/bash

set -eux

### --start_docs
## Prepare the undercloud for deploying the containerized compute node
## ===================================================================

## .. note:: In progress documentation is available at https://etherpad.openstack.org/p/tripleo-containers-work
## ::

## Prepare Your Environment
## ------------------------

## * Add an additional insecure registry if needed
## ::
{% if additional_insecure_registry|bool %}
{% if job.registry_ip_address_branch is defined %}
if egrep -q "^INSECURE_REGISTRY=.*{{ job.registry_ip_address_branch[release] }}:{{ local_registry_port }}.*" /etc/sysconfig/docker; then
    echo "/etc/sysconfig/docker contains the correct settings"
else
    echo "updating /etc/sysconfig/docker for internal registry"
    sudo sed -i \
        -e "s/^INSECURE_REGISTRY=\"\(.*\)\"/INSECURE_REGISTRY=\"\1 --insecure-registry {{ job.registry_ip_address_branch[release] }}:{{ local_registry_port }}\"/g" \
        /etc/sysconfig/docker
    sudo systemctl restart docker
fi
{% else %}
if egrep -q "^INSECURE_REGISTRY=.*{{docker_registry_host}}.*" /etc/sysconfig/docker; then
    echo "/etc/sysconfig/docker contains the correct settings"
else
    echo "updating /etc/sysconfig/docker for internal registry"
    sudo sed -i -e "s/^INSECURE_REGISTRY=\"\(.*\)\"/INSECURE_REGISTRY=\"\1 --insecure-registry {{ docker_registry_host }}\"/g" /etc/sysconfig/docker
    sudo systemctl restart docker
fi
{% endif %}
{% endif %}

PREPARE_ARGS=${PREPARE_ARGS:-"{{ prepare_service_env_args }}"}

## * Configure the {{ working_dir }}/containers-default-parameters.yaml, and
##   populate the docker registry. This is done automatically.
## ::

{% if release not in ['ocata', 'pike', 'queens'] and not mixed_upgrade|default(false)|bool %}
openstack tripleo container image prepare --verbose \
    --output-env-file {{ working_dir }}/containers-default-parameters.yaml \
    ${PREPARE_ARGS} \
    -e {{ working_dir }}/containers-prepare-parameter.yaml

{% else %}

openstack overcloud container image prepare \
    --output-images-file {{ working_dir }}/overcloud_containers.yaml \
    --output-env-file {{ working_dir }}/containers-default-parameters.yaml \
    ${PREPARE_ARGS} \
{% if job.consumer_job | default(false) | bool %}
    --namespace "{{ job.registry_ip_address_branch[release] }}:{{ local_registry_port }}/{{ docker_registry_namespace_used }}" \
{% else %}
    --namespace "{{ docker_registry_host }}/{{ docker_registry_namespace_used }}" \
{% endif %}
    --tag {{ container_build_id }} \
{% if docker_prep_prefix is defined %}
    --prefix "{{ docker_prep_prefix }}" \
{% endif %}
{% if docker_prep_suffix is defined %}
    --suffix "{{ docker_prep_suffix }}" \
{% endif %}
{% if docker_ceph_namespace is defined %}
    --set "ceph_namespace={{ docker_ceph_namespace }}" \
{% endif %}
{% if docker_ceph_image is defined %}
    --set "ceph_image={{ docker_ceph_image }}" \
{% endif %}
{% if docker_ceph_tag is defined %}
    --set "ceph_tag={{ docker_ceph_tag }}" \
{% endif %}
{% if custom_ceph_alertmanager_image is defined %}
    --set "ceph_alertmanager_image={{ custom_ceph_alertmanager_image }}" \
{% endif %}
{% if custom_ceph_alertmanager_namespace is defined %}
    --set "ceph_alertmanager_namespace={{ custom_ceph_alertmanager_namespace }}" \
{% endif %}
{% if custom_ceph_alertmanager_tag is defined %}
    --set "ceph_alertmanager_tag={{ custom_ceph_alertmanager_tag }}" \
{% endif %}
{% if custom_ceph_grafana_image is defined %}
    --set "ceph_grafana_image={{ custom_ceph_grafana_image }}" \
{% endif %}
{% if custom_ceph_grafana_namespace is defined %}
    --set "ceph_grafana_namespace={{ custom_ceph_grafana_namespace }}" \
{% endif %}
{% if custom_ceph_grafana_tag is defined %}
    --set "ceph_grafana_tag={{ custom_ceph_grafana_tag }}" \
{% endif %}
{% if custom_ceph_node_exporter_image is defined %}
    --set "ceph_node_exporter_image={{ custom_ceph_node_exporter_image }}" \
{% endif %}
{% if custom_ceph_node_exporter_namespace is defined %}
    --set "ceph_node_exporter_namespace={{ custom_ceph_node_exporter_namespace }}" \
{% endif %}
{% if custom_ceph_node_exporter_tag is defined %}
    --set "ceph_node_exporter_tag={{ custom_ceph_node_exporter_tag }}" \
{% endif %}
{% if custom_ceph_prometheus_image is defined %}
    --set "ceph_prometheus_image={{ custom_ceph_prometheus_image }}" \
{% endif %}
{% if custom_ceph_prometheus_namespace is defined %}
    --set "ceph_prometheus_namespace={{ custom_ceph_prometheus_namespace }}" \
{% endif %}
{% if custom_ceph_prometheus_tag is defined %}
    --set "ceph_prometheus_tag={{ custom_ceph_prometheus_tag }}" \
{% endif %}
{% if docker_openshift_namespace is defined %}
    --set openshift_namespace={{ docker_openshift_namespace }} \
{% endif %}
{% if docker_openshift_tag is defined %}
    --set openshift_tag={{ docker_openshift_tag }} \
{% endif %}
{% if docker_openshift_prefix is defined %}
    --set openshift_prefix={{ docker_openshift_prefix }} \
{% endif %}
{% if docker_openshift_cockpit_namespace is defined %}
    --set openshift_cockpit_namespace={{ docker_openshift_cockpit_namespace }} \
{% endif %}
{% if docker_openshift_cockpit_image is defined %}
    --set openshift_cockpit_image={{ docker_openshift_cockpit_image }} \
{% endif %}
{% if docker_openshift_cockpit_tag is defined %}
    --set openshift_cockpit_tag={{ docker_openshift_cockpit_tag }} \
{% endif %}
{% if docker_openshift_etcd_namespace is defined %}
    --set openshift_etcd_namespace={{ docker_openshift_etcd_namespace }} \
{% endif %}
{% if docker_openshift_etcd_image is defined %}
    --set openshift_etcd_image={{ docker_openshift_etcd_image }} \
{% endif %}
{% if docker_openshift_etcd_tag is defined %}
    --set openshift_etcd_tag={{ docker_openshift_etcd_tag }} \
{% endif %}
{% if docker_openshift_gluster_namespace is defined %}
    --set openshift_gluster_namespace={{ docker_openshift_gluster_namespace }} \
{% endif %}
{% if docker_openshift_gluster_image is defined %}
    --set openshift_gluster_image={{ docker_openshift_gluster_image }} \
{% endif %}
{% if docker_openshift_gluster_block_image is defined %}
    --set openshift_gluster_block_image={{ docker_openshift_gluster_block_image }} \
{% endif %}
{% if docker_openshift_gluster_tag is defined %}
    --set openshift_gluster_tag={{ docker_openshift_gluster_tag }} \
{% endif %}
{% if docker_openshift_heketi_namespace is defined %}
    --set openshift_heketi_namespace={{ docker_openshift_heketi_namespace }} \
{% endif %}
{% if docker_openshift_heketi_image is defined %}
    --set openshift_heketi_image={{ docker_openshift_heketi_image }} \
{% endif %}
{% if docker_openshift_heketi_tag is defined %}
    --set openshift_heketi_tag={{ docker_openshift_heketi_tag }} \
{% endif %}
{% if docker_openshift_cluster_monitoring_namespace is defined %}
    --set openshift_cluster_monitoring_namespace={{ docker_openshift_cluster_monitoring_namespace }} \
{% endif %}
{% if docker_openshift_cluster_monitoring_image is defined %}
    --set openshift_cluster_monitoring_image={{ docker_openshift_cluster_monitoring_image }} \
{% endif %}
{% if docker_openshift_cluster_monitoring_tag is defined %}
    --set openshift_cluster_monitoring_tag={{ docker_openshift_cluster_monitoring_tag }} \
{% endif %}
{% if docker_openshift_configmap_reload_namespace is defined %}
    --set openshift_configmap_reload_namespace={{ docker_openshift_configmap_reload_namespace }} \
{% endif %}
{% if docker_openshift_configmap_reload_image is defined %}
    --set openshift_configmap_reload_image={{ docker_openshift_configmap_reload_image }} \
{% endif %}
{% if docker_openshift_configmap_reload_tag is defined %}
    --set openshift_configmap_reload_tag={{ docker_openshift_configmap_reload_tag }} \
{% endif %}
{% if docker_openshift_prometheus_operator_namespace is defined %}
    --set openshift_prometheus_operator_namespace={{ docker_openshift_prometheus_operator_namespace }} \
{% endif %}
{% if docker_openshift_prometheus_operator_image is defined %}
    --set openshift_prometheus_operator_image={{ docker_openshift_prometheus_operator_image }} \
{% endif %}
{% if docker_openshift_prometheus_operator_tag is defined %}
    --set openshift_prometheus_operator_tag={{ docker_openshift_prometheus_operator_tag }} \
{% endif %}
{% if docker_openshift_prometheus_config_reload_namespace is defined %}
    --set openshift_prometheus_config_reload_namespace={{ docker_openshift_prometheus_config_reload_namespace }} \
{% endif %}
{% if docker_openshift_prometheus_config_reload_image is defined %}
    --set openshift_prometheus_config_reload_image={{ docker_openshift_prometheus_config_reload_image }} \
{% endif %}
{% if docker_openshift_prometheus_config_reload_tag is defined %}
    --set openshift_prometheus_config_reload_tag={{ docker_openshift_prometheus_config_reload_tag }} \
{% endif %}
{% if docker_openshift_prometheus_tag is defined %}
    --set openshift_prometheus_tag={{ docker_openshift_prometheus_tag }} \
{% endif %}
{% if docker_openshift_prometheus_alertmanager_tag is defined %}
    --set openshift_prometheus_alertmanager_tag={{ docker_openshift_prometheus_alertmanager_tag }} \
{% endif %}
{% if docker_openshift_prometheus_node_exporter_tag is defined %}
    --set openshift_prometheus_node_exporter_tag={{ docker_openshift_prometheus_node_exporter_tag }} \
{% endif %}
{% if docker_openshift_oauth_proxy_tag is defined %}
    --set openshift_oauth_proxy_tag={{ docker_openshift_oauth_proxy_tag }} \
{% endif %}
{% if docker_openshift_kube_rbac_proxy_namespace is defined %}
    --set openshift_kube_rbac_proxy_namespace={{ docker_openshift_kube_rbac_proxy_namespace }} \
{% endif %}
{% if docker_openshift_kube_rbac_proxy_image is defined %}
    --set openshift_kube_rbac_proxy_image={{ docker_openshift_kube_rbac_proxy_image }} \
{% endif %}
{% if docker_openshift_kube_rbac_proxy_tag is defined %}
    --set openshift_kube_rbac_proxy_tag={{ docker_openshift_kube_rbac_proxy_tag }} \
{% endif %}
{% if docker_openshift_kube_state_metrics_namespace is defined %}
    --set openshift_kube_state_metrics_namespace={{ docker_openshift_kube_state_metrics_namespace }} \
{% endif %}
{% if docker_openshift_kube_state_metrics_image is defined %}
    --set openshift_kube_state_metrics_image={{ docker_openshift_kube_state_metrics_image }} \
{% endif %}
{% if docker_openshift_kube_state_metrics_tag is defined %}
    --set openshift_kube_state_metrics_tag={{ docker_openshift_kube_state_metrics_tag }} \
{% endif %}
{% if docker_openshift_grafana_namespace is defined %}
    --set openshift_grafana_namespace={{ docker_openshift_grafana_namespace }} \
{% endif %}
{% if docker_openshift_grafana_tag is defined %}
    --set openshift_grafana_tag={{ docker_openshift_grafana_tag }} \
{% endif %}
    --push-destination {{ local_docker_registry_host }}:8787

openstack overcloud container image upload --debug --config-file {{ working_dir }}/overcloud_containers.yaml

{% if release in ['ocata', 'pike'] %}
## * Configure the {{ working_dir }}/containers-default-parameters.yaml to pull
##   from a local registry, this is done automatically.
## ::

    openstack overcloud container image prepare \
        --env-file {{ working_dir }}/containers-default-parameters.yaml \
        ${PREPARE_ARGS} \
        --namespace {{ local_docker_registry_host }}:8787/{{ docker_registry_namespace }} \
    {% if docker_prep_prefix is defined %}
        --prefix "{{ docker_prep_prefix }}" \
    {% endif %}
    {% if docker_prep_suffix is defined %}
        --suffix "{{ docker_prep_suffix }}" \
    {% endif %}
    {% if docker_ceph_namespace is defined %}
        --set "ceph_namespace={{ docker_ceph_namespace }}" \
    {% endif %}
    {% if docker_ceph_image is defined %}
        --set "ceph_image={{ docker_ceph_image }}" \
    {% endif %}
    {% if docker_ceph_tag is defined %}
        --set "ceph_tag={{ docker_ceph_tag }}" \
    {% endif %}
        --tag {{ container_build_id }}
{% endif %}

{% if update_containers|bool and not use_overcloud_mixed_upgrade|default(false)|bool %}
# See https://github.com/imain/container-check for script and documentation

## * update the containers with yum updates from OpenStack repos.  ( delorean-current, dependent patches built by DLRN )
## ::

packages_for_update="$(repoquery --disablerepo='*' --enablerepo={{ update_containers_repo }} --qf %{NAME} -a 2>{{ working_dir }}/repoquery.err.log | sort -u | xargs)"

if [[ -n "$packages_for_update" ]]; then
    openstack overcloud container image prepare \
        --output-images-file {{ working_dir }}/containers_update.yaml \
        ${PREPARE_ARGS} \
        --exclude ceph \
        --exclude openshift \
        --exclude kubernetes \
        --namespace {{ local_docker_registry_host }}:8787/{{ docker_registry_namespace }} \
    {% if docker_prep_prefix is defined %}
        --prefix "{{ docker_prep_prefix }}" \
    {% endif %}
    {% if docker_prep_suffix is defined %}
        --suffix "{{ docker_prep_suffix }}" \
    {% endif %}
        --tag {{ container_build_id }}

    container-check -u \
        -c {{ working_dir }}/containers_update.yaml \
        -p {{ container_process_count }} \
        -k $packages_for_update
fi

{% endif %}
{% endif %}

echo "============================="
echo "Containers default parameters:"
cat {{ working_dir }}/containers-default-parameters.yaml
echo "============================="

## * Get the journal logs for docker
## ::

sudo journalctl -u docker > docker_journalctl.log

### --stop_docs
